"""爬取西祠代理ip"""

import requests
from lxml import etree
from flask import Flask


app = Flask(__name__)
@app.route('/')




class GetProxies(object):
    def __init__(self):
        self.url = 'https://www.xicidaili.com/'
        self.headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36'}
        self.proxies = {'http': 'http://121.233.207.120:9999'}

    def get_proxies(self):
        resp = requests.get(self.url, headers=self.headers, proxies=self.proxies)
        html_str = resp.text
        html = etree.HTML(html_str)
        ip_list = html.xpath('//tr/td[2]/text()')
        port_list = html.xpath('//tr/td[3]/text()')
        is_high_annoy_list = html.xpath('//tr/td[5]/text()')
        protocol_list = html.xpath('//tr/td[6]/text()')

        for index, ip in enumerate(ip_list):
            port = port_list[index]
            is_high_annoy = is_high_annoy_list[index]
            protocol = protocol_list[index].lower()
            if is_high_annoy == '高匿' and protocol != 'socks4/5':
                # {'http': 'http://121.233.207.120:9999'}
                proxies = {'{}'.format(protocol):'{}://{}:{}'.format(protocol, ip, port)}

                print(proxies)


if __name__ == '__main__':
    # g = GetProxies()
    # g.get_proxies()
    app.run()

